//
//  CYLineBtn.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYLineBtn.h"
@interface CYLineBtn(){
    
    CGFloat _height;
    UIColor *_color;
}
@property (nonatomic, strong) UIView *line;

@end
@implementation CYLineBtn

- (instancetype)initWithTitle:(NSString *)title color:(UIColor *)color size:(CGFloat)size height:(CGFloat)height{
    self = [super init];
    if (self) {
        //        self.backgroundColor = RGBA(arc4random()% 256, arc4random() % 256, arc4random() % 256, 1);
        _height = height;
        _color = color;
        self.titleLabel.font = [UIFont systemFontOfSize:size];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:color forState:UIControlStateSelected];
        [self.titleLabel sizeToFit];
        
    }
    return self;
}

- (UIView *)line{
    if (!_line) {
        _line = [[UIView alloc]init];
        [self addSubview:_line];
        
        CGSize size = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.mas_bottom).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(size.width, _height));
        }];
    }
    return _line;
}

- (void)setCYselected:(BOOL)CYselected{
    if (_CYselected!= CYselected)
        _CYselected = CYselected;
    self.selected = CYselected;
    self.line.backgroundColor = (CYselected)?_color:[UIColor whiteColor];
    [self setTitleColor:(CYselected)?_color:[UIColor blackColor] forState:UIControlStateNormal];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
